//
//  RightViewController.m
//  南京大牌档
//
//  Created by student on 14-1-9.
//  Copyright (c) 2014年 无锡第三团队. All rights reserved.
//

#import "RightViewController.h"
#import "RightCell.h"
@interface RightViewController ()

@end

@implementation RightViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor=[UIColor lightGrayColor];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(translateNum:) name:@"translateCellData" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNum:) name:@"translateRightCellData" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	billArray=[[NSMutableArray alloc]init];
    numArray=[[NSMutableArray alloc]init];
}

-(void)initTableView
{
    if (WXHLOSVersion()>=7.0) {
        rightTV=[[UITableView alloc]initWithFrame:CGRectMake(30, 20, 290, WXHLApplicationBounds().size.height-30) style:UITableViewStylePlain];
    }
    else{
        rightTV=[[UITableView alloc]initWithFrame:CGRectMake(30, 0, 290, WXHLApplicationBounds().size.height-30) style:UITableViewStylePlain];
    }
    
    
        rightTV.dataSource=self;
    rightTV.delegate=self;
    rightTV.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    rightTV.backgroundColor=[UIColor lightGrayColor];
    
    UIView *rightTitleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, 80)];
    rightTitleView.backgroundColor=[UIColor lightGrayColor];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 290, 30)];
    titleLabel.textAlignment=1;
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.font=[UIFont boldSystemFontOfSize:15];
    titleLabel.text=@"清单";
    [rightTitleView addSubview:titleLabel];
    
    UILabel *imgLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 50, 30)];
    imgLabel.textAlignment=1;
    imgLabel.backgroundColor=[UIColor clearColor];
    imgLabel.font=[UIFont boldSystemFontOfSize:10];
    imgLabel.text=@"图片";
    [rightTitleView addSubview:imgLabel];
    
    UILabel *foodTitleLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
    foodTitleLabel.textAlignment=1;
    foodTitleLabel.backgroundColor=[UIColor clearColor];
    foodTitleLabel.font=[UIFont boldSystemFontOfSize:10];
    foodTitleLabel.text=@"名称";
    [rightTitleView addSubview:foodTitleLabel];
    
    UILabel *numLabel=[[UILabel alloc]initWithFrame:CGRectMake(190, 50, 20, 30)];
    numLabel.textAlignment=1;
    numLabel.backgroundColor=[UIColor clearColor];
    numLabel.font=[UIFont boldSystemFontOfSize:10];
    numLabel.text=@"数量";
    [rightTitleView addSubview:numLabel];
    
    UILabel *priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(250, 50, 40, 30)];
    priceLabel.textAlignment=1;
    priceLabel.backgroundColor=[UIColor clearColor];
    priceLabel.font=[UIFont boldSystemFontOfSize:10];
    priceLabel.text=@"单价";
    [rightTitleView addSubview:priceLabel];
    
    UIView *rightFootView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, 110)];
    rightFootView.backgroundColor=[UIColor lightGrayColor];
    
    UILabel *noticeeLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 290, 10)];
    noticeeLabel.text=@"价格仅供参考，具体以店堂出单为准。";
    noticeeLabel.backgroundColor=[UIColor clearColor];
    noticeeLabel.textAlignment=0;
    noticeeLabel.font=[UIFont boldSystemFontOfSize:7];
    [rightFootView addSubview:noticeeLabel];
    
    UILabel *totalPriceLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, 290, 30)];
    totalPriceLabel.textAlignment=0;
    int sum=0;
    for (int i=0; i<billArray.count; i++) {
        sum=sum+[billArray[i][3] integerValue]*[numArray[i] integerValue];
    }
    totalPriceLabel.backgroundColor=[UIColor clearColor];
    totalPriceLabel.text=[NSString stringWithFormat:@"合计:%d元",sum];
    totalPriceLabel.font=[UIFont boldSystemFontOfSize:15];
    [rightFootView addSubview:totalPriceLabel];
    
    UITextField *requireTF=[[UITextField alloc]initWithFrame:CGRectMake(10, 40, 270, 30)];
    requireTF.backgroundColor=[UIColor whiteColor];
    requireTF.font=[UIFont boldSystemFontOfSize:10];
    requireTF.borderStyle=UITextBorderStyleRoundedRect;
    requireTF.placeholder=@"您若有另外要求，请在此留言！";
    requireTF.returnKeyType=UIReturnKeyDone;
    requireTF.delegate=self;
    [rightFootView addSubview:requireTF];
    
    
    UIButton *confirmButton=[[UIButton alloc]initWithFrame:CGRectMake(90, 90, 110, 20)];
    confirmButton.layer.cornerRadius=5;
    confirmButton.tag=4;
    [confirmButton setTitle:@"下单" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    confirmButton.backgroundColor=[UIColor whiteColor];
        [confirmButton addTarget:self action:@selector(confirmButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [rightFootView addSubview:confirmButton];
    
    rightTV.tableHeaderView=rightTitleView;
    rightTV.tableFooterView=rightFootView;
    [self.view addSubview:rightTV];
}

-(void)confirmButtonPressed:(UIButton *)button
{
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"确认下单?" message:@"确定后订单将不可修改" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
//        ZBarReaderViewController *reader = [ZBarReaderViewController new];
//        reader.readerDelegate = self;
//        reader.supportedOrientationsMask = ZBarOrientationMaskAll;
//        
//        ZBarImageScanner *scanner = reader.scanner;
//        [scanner setSymbology: ZBAR_I25
//                       config: ZBAR_CFG_ENABLE
//                           to: 0];
//        
//
//        [self presentModalViewController: reader animated: YES];


    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    return YES;
}

//-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    id<NSFastEnumeration> results =
//    [info objectForKey: ZBarReaderControllerResults];
//    ZBarSymbol *symbol = nil;
//    for(symbol in results)
//        break;
//        NSLog(@"%@",symbol.data);
//    
//
////    resultImage.image =
////    [info objectForKey: UIImagePickerControllerOriginalImage];
//    
//    [picker dismissModalViewControllerAnimated: YES];
//
//}

-(void)translateNum:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];
    
    NSArray *array=[dic objectForKey:@"Array"];
    
    [billArray addObject:array];
    [numArray addObject:@1];
    if (billArray.count!=1) {
        for (int i=0; i<billArray.count-1; i++) {
             if ([array isEqualToArray:billArray[i]]) {
                 [billArray removeObjectAtIndex:billArray.count-1];
                 [numArray removeObjectAtIndex:numArray.count-1];
                  numArray[i]=[NSNumber numberWithInt:[numArray[i] integerValue]+1];
                }
            }
        }
    [rightTV removeFromSuperview];
    [self initTableView];

    
}

-(void)changeNum:(NSNotification *)notification
{
    NSDictionary *dic = [notification userInfo];
    
    NSNumber *tag=[dic objectForKey:@"Tag"];
    NSArray  *array=[dic objectForKey:@"Array"];

    if ([tag integerValue]==0) {
        for (int i=0; i<billArray.count; i++) {
            if ([array isEqualToArray:billArray[i]]&&[numArray[i] integerValue]==1) {
                [billArray removeObject:array];
                [numArray removeObjectAtIndex:i];

            }
            else if([array isEqualToArray:billArray[i]]){
               numArray[i]=[NSNumber numberWithInt:[numArray[i] integerValue]-1];
            }
                
        }
    
    [rightTV removeFromSuperview];
    [self initTableView];
    }
    else if([tag integerValue]==1){
        for (int i=0; i<billArray.count; i++)
            if ([array isEqualToArray:billArray[i]]) {
                numArray[i]=[NSNumber numberWithInt:[numArray[i] integerValue]+1];
            }
        [rightTV removeFromSuperview];
        [self initTableView];
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return billArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer=@"Cell";
    RightCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if(cell==nil)
    {
        cell=[[RightCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        UIView *view=[[UIView alloc]init];
        view.backgroundColor=[UIColor whiteColor];
        cell.backgroundView=view;
    }
  
    [cell.billImg setImageWithURL:[NSURL URLWithString:billArray[indexPath.row][5]]];
    cell.billTitle.text=billArray[indexPath.row][1];
    cell.billNum.text=[NSString stringWithFormat:@"%@",numArray[indexPath.row]];
    cell.billPrice.text=billArray[indexPath.row][3];
    cell.billCellArray=billArray[indexPath.row];
    return cell;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
